{% extends "admin_base.html" %}
{% block bodycontent %}
<style type="text/css">
.row_number {
  font-weight: bold;
}
.missing {
  background-color: red;
}
.invalid {
  background-color: red;
}
</style>

<h1>Import Work Orders by CSV</h1>

<p>CSV file "{{ csv_file_obj.filename }}" contains {{ annotated_rows|length }} row(s) &ndash; {{ valid_row_count}} row(s) are valid; {{ invalid_row_count }} row(s) are invalid.</p>
<p>Event selected: {{ event.name }}</p>

<h2>Save Options</h2>

<p>Of {{ valid_row_count }} valid row(s), {{ csv_file_obj.saved_count }} have been saved.</p>

{% if valid_row_count and valid_row_count > csv_file_obj.saved_count %}
  <form method="post">
    <input type="hidden" name="csv_id" value="{{ csv_id }}">
    <input type="hidden" name="choose_event" value="{{ event.key().id() }}">
    <button type="submit" name="action" value="write" onclick="return confirm('Are you sure you want to save the valid rows to the database?')">Save valid rows</button>
  </form>
{% else %}
  <p>There are no valid rows to save.</p>
{% endif %}

<h2>Download Options</h2>

{% if invalid_row_count %}
  <p>There are {{ invalid_row_count }} invalid row(s) available to download.</p>
  <form method="post" action="/admin-import-csv/active/invalids.csv">
    <input type="hidden" name="csv_id" value="{{ csv_id }}">
    <input type="hidden" name="choose_event" value="{{ event.key().id() }}">
    <button type="submit" name="action" value="download_invalid_csv">Download invalid rows as CSV</button>
  </form>
{% else %}
  <p>There are no invalid rows to download.</p>
{% endif %}

<h2>Data Received</h2>

{% for annotated_row in annotated_rows %}

  {% set num = annotated_row['num'] %}
  {% set row = annotated_row['row'] %}
  {% set validation = annotated_row['validation'] %}
  {% set geocoded_address = annotated_row['geocoded_address']%}
  {% if not validation['row_length_ok'] %}
    <p class="row_number">Row {{ num }}: <span style="color: red">error - wrong length</span></p>
  {% else %}
    <p class="row_number">Row {{ num}}: </p>
    <table border="1">
    {% if validation['contains_example_data'] %}
      <tr>
        <td class="invalid">Row contains example data</td>
      </tr>
    {% endif %}
    {% if validation['similar'] %}
      <tr>
        <td class="invalid" colspan="2">
          Row is too similar to existing work order {{ validation['similar'] }}
        </td>
      </tr>
    {% endif %}
    {% for field_name in validation['missing_fields'] %}
      <tr>
        <td class="invalid">Missing</td>
        <td>{{ field_name }}</td>
      </tr>
    {% endfor %}
    {% for field_name, field_value in zip(field_names, row) %}
      {% if field_value or (geocoded_address and field_name in geocoded_address) %}
        {% set invalid = (field_name in validation['invalid_fields']) %}
        <tr>
          <th style="width: 10%">{{ field_name }}</th>
          <td class="{% if invalid %}invalid{% endif %}" style="width:40%">
            {% if field_value %}
              {{ field_value }}
            {% elif field_name in geocoded_address %}
              <b>{{ geocoded_address[field_name] }}</b> <i>(geocoded)</i>
            {% endif %}
          </td>
          {% if invalid %}
            {% set msg = validation['invalid_fields'][field_name] %}
            {# currently disabled:: <td>{{ form[field_name] }}</td> #}
            <td>{{ msg }}</td>
          {% elif field_name == 'address' and validation['address_geocodes_ok'] == None %}
            <td>(Did not geocode due to other errors)</td>
          {% elif field_name == 'address' and validation['address_geocodes_ok'] %}
            <td class="ok">Address geocodes ok</td>
          {% elif field_name == 'address' and validation['geocoder_worked'] and not validation['address_geocodes_ok'] %}
            <td class="invalid">Address does not geocode</td>
          {% elif field_name == 'address' and not validation['geocoder_worked'] %}
            <td class="invalid">Geocoder failed - rerun at a later time</td>
          {% endif %}
        </tr>
      {% endif %}
    {% endfor %}
    </table>
  {% endif %}
{% endfor %}

{% endblock %}
